<template>
  <div class="home_box">
    <!-- 主页容器 -->
    <el-container class="home_container">
      <!-- 头部 -->
      <el-header class="home_head">
        <div class="logoBox">
          <img src="../assets/logo.png" />
          <span>后台管理项目</span>
        </div>
        <el-button type="info" @click="signOut">退出</el-button>
      </el-header>
      <!-- 内容 -->
      <el-container>
        <!-- 左侧导航栏 -->
        <el-aside
          :width="isCollapse ? 64 + 'px' : 200 + 'px'"
          class="home_left"
        >
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#606266"
            text-color="#fff"
            active-text-color="#409EFF"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
          >
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + item2.path"
                v-for="item2 in item.children"
                :key="item2.id"
                @click="saveNavState('/' + item2.path)"
              >
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ item2.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <!-- 右侧主体部分 -->
        <el-main>
          <keep-alive>
            <router-view></router-view>
          </keep-alive>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 数据列表
      menuList: [],
      // 字体列表
      iconObj: {
        125: 'iconfont icon-yonghuguanli',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdanguanli',
        145: 'iconfont icon-shujutongji'
      },
      // 是否折叠
      isCollapse: false,
      // 激活的链接
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    signOut() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取所有的菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        // const textPath = { path: 'text' }
        // res.data.push(textPath)
        this.menuList = res.data
        // console.log('push之后', res.data)
      }
    },
    // 点击按钮切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style scoped>
.home_head {
  background-color: #000000;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  padding: 0 30px;
}
.home_head .logoBox {
  display: flex;
  align-items: center;
}
.home_head img {
  width: 50px;
  margin-right: 20px;
}
.home_left {
  background-color: #606266;
}
.home_box {
  height: 100%;
}
.home_container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background-color: rgb(196, 189, 189);
  text-align: center;
  line-height: 30px;
  font-size: 14px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
